package com.example.smartcitydemo.ui.person

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DividerItemDecoration
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.BUS_ORDER_STATUS
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.BusOrderInfo
import com.example.smartcitydemo.databinding.FragmentHomeNewsBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.viewModel.person.PersonViewModel

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.person
 * @NAME: Caleb.F
 * @DATE: 2021/11/11 14:23
 * @Email: 1919796283@qq.com
 */
class BusOrderDetailFragment : BaseFragment<FragmentHomeNewsBinding>(R.layout.fragment_home_news) {
    val personViewModel: PersonViewModel by viewModels()
    val myOrderDetailAdapter: MyBusOrderDetailAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        MyBusOrderDetailAdapter().apply {
            init()
            setDiffCallback(BusOrderInfo.callback)
        }
    }

    override fun initData() {
        arguments?.let {
            val busStatus = it.getInt(BUS_ORDER_STATUS)
            personViewModel.getBusOrder(busStatus)
        }
    }

    override fun initView(bindView: FragmentHomeNewsBinding, savedInstanceState: Bundle?) {
        bindView.recyclerNews.run {
            adapter = myOrderDetailAdapter
            addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        }
    }

    override fun addObservers(bindView: FragmentHomeNewsBinding) {
        personViewModel.busOrderList.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                myOrderDetailAdapter.setDiffNewData(it.data!!.toMutableList())
            }
        }
    }

    companion object {
        fun getInstance(statusId: Int): BusOrderDetailFragment {
            return BusOrderDetailFragment().apply {
                arguments = Bundle().apply {
                    putInt(BUS_ORDER_STATUS, statusId)
                }
            }
        }
    }
}